package com.highspeed.bdk.gateway.socket.analyze.message.preset_deploy.resolver;

import com.highspeed.bdk.gateway.Util;
import com.highspeed.bdk.gateway.socket.analyze.message.preset_deploy.base.MessageResolver;

import java.util.Map;

public class FunLinkInfoResolver extends MessageResolver {

    @Override
    protected Map doResolveBody(byte[] response) {
        int numLinks = Util.getInt(response, getOffset());
        offset4();

        //如果链路条数小于等于0，则结束
        if (numLinks <= 0) {
            return getContainer();
        }

        logger.info("信号名称：APP链路条数............" + numLinks);
        int appId = Util.getInt(response, getOffset());
        offset4();
        logger.info("信号名称：应用组件ID............" + appId);
        String appName = Util.subBytesToString(response, getOffset(), 32);
        offsetN(32);

        //组件信息，直接跳过组件信息
        offsetN(160);
        logger.info("信号名称：应用组件名称............" + appName);
        for (int i = 0; i < numLinks; i++) {
            int numLink = Util.getInt(response, getOffset());
            logger.info("numLink............" + numLink);
            offset4();
            appId = Util.getInt(response, getOffset());
            offset4();
            logger.info("信号名称：应用组件ID............" + appId);

            appName = Util.subBytesToString(response, getOffset(), 32);
            offsetN(32);
            ;
            logger.info("信号名称：应用组件名称............" + appName);
            AppPortSTResolver portSTResolver = new AppPortSTResolver();
            portSTResolver.setContainer(getContainer());
            portSTResolver.doResolveBody(response);
            portSTResolver.doResolveBody(response);

            short transferType = Util.getShort(response, getOffset());
            offset2();
            logger.info("信号名称：传输类型............" + transferType);

            long matID = Util.getShort(response, getOffset());
            offset2();
            logger.info("信号名称：多播地址............" + matID);

            long descRIOAddrs = Util.getUnsignedInt(response, getOffset());
            offset4();
            logger.info("信号名称：DESC RIO地址............" + descRIOAddrs);

            long dataRIOAddrs = Util.getUnsignedInt(response, getOffset());
            offset4();
            logger.info("信号名称：DATA RIO地址 ............" + dataRIOAddrs);

            long slotSize = Util.getUnsignedInt(response, getOffset());
            offset4();
            logger.info("信号名称：缓冲区大小..........." + slotSize);

            int slotNum = Util.getInt(response, getOffset());
            offset4();
            logger.info("信号名称：缓冲区个数............" + slotNum);

            long flowControlYesOrNo = Util.getUnsignedByte(response, getOffset());
            offset1();
            logger.info("信号名称：是否流控............" + flowControlYesOrNo);

            long sendDescYesOrNo = Util.getUnsignedByte(response, getOffset());
            offset1();
            logger.info("信号名称：是否发送数据描述............" + sendDescYesOrNo);

            long sendNotifyYesOrNo = Util.getUnsignedByte(response, getOffset());
            offset1();
            logger.info("信号名称：是否提醒消息到来............" + sendNotifyYesOrNo);

            long reliableYesOrNo = Util.getUnsignedByte(response, getOffset());
            offset1();
            logger.info("信号名称：是否可靠传输............" + reliableYesOrNo);
        }
        return getContainer();
    }
}
